package com.sc.byahi.user.client;

import java.util.Arrays;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;

@Deprecated
public class PhotoUploadPopup extends FlowPanel {
	public PhotoUploadPopup(final Long profileId, final boolean isMap) {
		final FormPanel formPanel = new FormPanel();
		add(formPanel);

		GreetingService.Util.getInstance().getUploadURL(new AsyncCallback<String>() {
			public void onFailure(Throwable error) {
			}

			public void onSuccess(String url) {
				formPanel.setAction(url); // <<<- BLOBSTORE.REQUESTUPLOADURL HERE!
			}
		});

		formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		formPanel.setMethod(FormPanel.METHOD_POST);

		final FlexTable ft = new FlexTable();
		int r = 0;
		formPanel.setWidget(ft);

		Hidden hiddenBox = new Hidden();
		hiddenBox.setName("profileid");
		hiddenBox.setID("profileid");
		hiddenBox.setValue(profileId.toString());
		ft.setWidget(r++, 0, hiddenBox);

		Hidden hidden = new Hidden("email");
		hidden.setValue(AppController.get().getOwner().getId());
		ft.setWidget(r++, 0, hidden);

		final FileUpload photo1 = new FileUpload();
		photo1.setName("photo1");
		ft.setWidget(r++, 0, photo1);

		final FileUpload photo2 = new FileUpload();
		photo2.setName("photo2");
		ft.setWidget(r++, 0, photo2);

		final FileUpload photo3 = new FileUpload();
		photo3.setName("photo3");
		ft.setWidget(r++, 0, photo3);

		final FileUpload photo4 = new FileUpload();
		photo4.setName("photo4");
		ft.setWidget(r++, 0, photo4);

		final Label error = new Label();
		error.setText("");
		ft.setWidget(r++, 0, error);

		// SubmitButton button = new SubmitButton("Submit");
		Button button = new Button("Submit");
		ft.setWidget(r, 0, button);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				// This event is fired just before the form is submitted. We can take this opportunity to perform validation.
				if (photo1.getFilename().length() == 0 && photo2.getFilename().length() == 0 && photo3.getFilename().length() == 0 && photo4.getFilename().length() == 0) {
					error.setText("Please upload at least one photo.");
				} else {
					if (photo1.getFilename() != null && photo1.getFilename().trim().length() == 0) {
						ft.remove(photo1);
					}
					if (photo2.getFilename() != null && photo2.getFilename().trim().length() == 0) {
						ft.remove(photo2);
					}
					if (photo3.getFilename() != null && photo3.getFilename().trim().length() == 0) {
						ft.remove(photo3);
					}
					if (photo4.getFilename() != null && photo4.getFilename().trim().length() == 0) {
						ft.remove(photo4);
					}
					formPanel.submit();
				}
			}
		});
		// Add an event handler to the form.
		formPanel.addSubmitHandler(new FormPanel.SubmitHandler() {
			public void onSubmit(SubmitEvent event) {
			}
		});
		formPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			public void onSubmitComplete(SubmitCompleteEvent event) {
				// When the form submission is successfully completed, this event is
				// fired. Assuming the service returned a response of type text/html,
				// we can get the result text here (see the FormPanel documentation for
				// further explanation).
				// History.newItem("myprofiles", true);
				// Window.alert(event.getResults());
				AppController.get().profileApprovalChanged(profileId, false);
				AppController.get().profileUpdated(profileId, isMap);
				UIUtil.showDialog(Arrays.asList("Pictures uploaded successfully"), "Pic Upload");
				((PopupPanel) formPanel.getParent().getParent()).hide();
				// System.out.println(event.getResults());
			}
		});

		Button close = new Button("Close");
		ft.setWidget(r++, 1, close);
		close.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				((PopupPanel) formPanel.getParent().getParent()).hide();// TODO Auto-generated method stub
			}
		});

	}
}
